package com.lin.mybatis.jsql;

import net.sf.jsqlparser.expression.*;
import net.sf.jsqlparser.expression.operators.relational.*;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.select.SubSelect;
import org.junit.jupiter.api.Test;

/**
 * @author linjiang
 * @date 2024/6/12
 * @fn
 */
public class TestJSqlExpression {

    @Test
    public void testLongValue() {
        // LongValue -->  如 where id = 25，会抽取sql语句中表达式的值（25）。
        LongValue longValue = new LongValue(25);
        System.out.println(longValue);
    }

    @Test
    public void testStringValue() {
        // StringValue --> 如 where name = "我是你爹！"，抽取值（我是你爹！）
        StringValue stringValue = new StringValue("samN'dkas");
        System.out.println(stringValue);

        StringValue instance = new StringValue("'*''*'");
        String result = instance.getNotExcapedValue();
        System.out.println(result);

        instance = new StringValue("N'test'");
        System.out.println(instance.getPrefix());
        System.out.println(instance.getValue());
    }


    @Test
    public void testNullValue() {
        // NullValue --> 如 where name = null，抽取值（null）
        NullValue nullValue = new NullValue();
        System.out.println(nullValue);
    }

    @Test
    public void testIsNullValue() {
        // IsNullExpression -> 如 name is null
        IsNullExpression isNullExpression = new IsNullExpression();
        isNullExpression.setLeftExpression(new Column("name"));
        System.out.println(isNullExpression);
    }

    @Test
    public void testInExpression() {
        // InExpression --> 如 id in (1,2,3,4,5)
        InExpression inExpression = new InExpression();
        inExpression.setLeftExpression(new Column("id"));

        ItemsList list = new ExpressionList(new LongValue(1), new LongValue(2), new LongValue(3), new LongValue(4), new LongValue(5));
        inExpression.setRightItemsList(list);

        System.out.println(inExpression);
    }

    @Test
    public void testBetween() {
        // Between --> 如 over_time BETWEEN '2024-06-12' AND '2024-06-13'
        Between between = new Between();
        between.setLeftExpression(new Column("over_time"));

        between.setBetweenExpressionStart(new StringValue("2024-06-12"));
        between.setBetweenExpressionEnd(new StringValue("2024-06-13"));
        System.out.println(between);
    }

    @Test
    public void testSubSelect() {
        // SubSelect --> 子查询
        SubSelect select = new SubSelect();
        System.out.println(select);
    }

    @Test
    public void testEqualsTo() {
        // EqualsTo -> 如 id = 25
        EqualsTo equalsTo = new EqualsTo();
        equalsTo.setLeftExpression(new Column("id"));
        equalsTo.setRightExpression(new LongValue(25));
        System.out.println(equalsTo);
    }

    @Test
    public void testArrayExpression() {
        // ArrayExpression -> 如 id[1]
        Expression obj = new Column("id");
        Expression index = new LongValue(1);
        ArrayExpression arrayExpression = new ArrayExpression(obj,index);
        System.out.println(arrayExpression);
    }
}
